<template>
  <div class="goodList">
<!--    <Form ref="formValidate" :model="formValidate" :label-width="120" label-position="right" class="tabform">-->
<!--      <Row type="flex" :gutter="24">-->
<!--        <Col v-bind="grid">-->
<!--          <FormItem label="用户名称：" label-for="store_name">-->
<!--            <Input-->
<!--              search-->
<!--              enter-button-->
<!--              placeholder="请输入用户名称"-->
<!--              v-model="formValidate.store_name"-->
<!--              style="width: 250px"-->
<!--              @on-search="userSearchs"-->
<!--            />-->
<!--          </FormItem>-->
<!--        </Col>-->
<!--      </Row>-->
<!--    </Form>-->
    <Table
      ref="table"
      no-data-text="暂无数据"
      @on-selection-change="changeCheckbox"
      no-filtered-data-text="暂无筛选结果"
      max-height="400"
      :columns="columns5"
      :data="tableList"
      :loading="loading"
    >
      <template slot-scope="{ row, index }" slot="image">
        <div class="tabBox_img" v-viewer>
          <img v-lazy="row.image" />
        </div>
      </template>
    </Table>
    <div class="acea-row row-right page">
      <Page :total="total" show-elevator show-total @on-change="pageChange" :page-size="formValidate.limit" />
    </div>
    <div class="footer" slot="footer" v-if="many === 'many' && !diy">
      <Button type="primary" size="large" :loading="modal_loading" long @click="ok">提交</Button>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { treeListApi, changeListApi } from "@/api/product";
import { liveGoods } from "@/api/live";
import { getProductList } from "@/api/diy";
import { adminUserList } from "@/api/user";

export default {
  name: "roleAdminList",
  props: {
    is_new: {
      type: String,
      default: ""
    },
    type: {
      type: Number,
      default: 0
    },
    diy: {
      type: Boolean,
      default: false
    },
    ischeckbox: {
      type: Boolean,
      default: true
    },
    liveStatus: {
      type: Boolean,
      default: false
    },
    isLive: {
      type: Boolean,
      default: false
    },
    isdiy: {
      type: Boolean,
      default: false
    },
    datas: {
      type: Object,
      default: function() {
        return {};
      }
    }
  },
  data() {
    return {
      modal_loading: false,
      treeSelect: [],
      formValidate: {
        page: 1,
        limit: 15,
        cate_id: "",
        store_name: "",
        is_new: this.is_new
      },
      total: 0,
      modals: false,
      loading: false,
      grid: {
        xl: 10,
        lg: 10,
        md: 12,
        sm: 24,
        xs: 24
      },
      tableList: [],
      currentid: 0,
      productRow: {},
      columns5: [
        {
          title: "用户ID",
          key: "id"
        },
        {
          title: "公司名称",
          key: "sqmc"
        },
        {
          title: "姓名",
          key: "name"
        },
        {
          title: "联系电话",
          key: "phone"
        }
      ],
      images: [],
      many: "",
      goodType: "",
      goodList: [
        {
          activeValue: 0,
          title: "商品列表"
        },
        {
          activeValue: "4",
          title: "热门榜单"
        },
        {
          activeValue: "5",
          title: "首发新品"
        },
        {
          activeValue: "6",
          title: "促销单品"
        },
        {
          activeValue: "7",
          title: "精品推荐"
        }
      ]
    };
  },
  computed: {},
  created() {
    let radio = {
      width: 60,
      align: "center",
      render: (h, params) => {
        let id = params.row.id;
        let flag = false;
        if (this.currentid === id) {
          flag = true;
        } else {
          flag = false;
        }
        let self = this;
        return h("div", [
          h("Radio", {
            props: {
              value: flag
            },
            on: {
              "on-change": () => {
                self.currentid = id;
                this.productRow = params.row;
                this.$emit("getRoleAdminId", this.productRow);
                if (this.productRow.id) {
                  if (this.$route.query.fodder === "image") {
                    /* eslint-disable */
                    let imageObject = {
                      image: this.productRow.image,
                      product_id: this.productRow.id
                    };
                    form_create_helper.set("image", imageObject);
                    form_create_helper.close("image");
                  }
                } else {
                  this.$Message.warning("请先选择用户");
                }
              }
            }
          })
        ]);
      }
    };

    let checkbox = {
      type: "selection",
      width: 60,
      align: "center"
    };
    let many = "";
    if (this.ischeckbox) {
      many = "many";
    } else {
      many = this.$route.query.type;
    }
    this.many = many;
    if (many === "many") {
      this.columns5.unshift(checkbox);
    } else {
      this.columns5.unshift(radio);
    }
  },
  mounted() {
    // this.goodsCategory();
    // if (this.diy) {
    //   this.productList();
    // } else {
    // }
    this.getList();
  },
  methods: {
    productList() {
      let data = {
        page: this.formValidate.page,
        limit: this.formValidate.limit,
        cate_id: this.formValidate.cate_id,
        store_name: this.formValidate.store_name,
        type: this.type ? this.type : this.goodType
      };
      this.loading = true;
      getProductList(data)
        .then((res) => {
          let data = res.data;
          this.tableList = data.list;
          this.total = res.data.count;
          this.loading = false;
        })
        .catch((res) => {
          this.loading = false;
          this.$Message.error(res.msg);
        });
    },
    changeCheckbox(selection) {
      let images = [];
      selection.forEach(function(item) {
        let imageObject = {
          name: item.name,
          id: item.id,
          sqmc: item.sqmc,
          phone: item.phone
        };
        images.push(imageObject);
      });
      this.images = images;
      this.diyVal = selection;
      // this.$emit('getProductDiy', selection);
    },
    // 商品分类；
    goodsCategory() {
      treeListApi(1)
        .then((res) => {
          this.treeSelect = res.data;
        })
        .catch((res) => {
          this.$Message.error(res.msg);
        });
    },
    pageChange(index) {
      this.formValidate.page = index;
      if (this.diy) {
        this.productList();
      } else {
        this.getList();
      }
    },
    // 列表
    getList() {
      this.loading = true;
      let params = {
        current: 1,
        size: 1000
      };
      adminUserList(params)
        .then(async (res) => {
          let data = res.data;
          this.tableList = data.records;
          this.total = data.total;
          this.loading = false;
          this.$nextTick(() => {
            // this.setChecked();
          });
        })
        .catch((res) => {
          this.loading = false;
          this.$Message.error("获取失败");
        });

      // if (!this.liveStatus) {
      //   if (this.isLive) {
      //     this.formValidate.is_live = 1;
      //   }
      //   changeListApi(this.formValidate)
      //     .then(async (res) => {
      //       let data = res.data;
      //       this.tableList = data.list;
      //       this.total = res.data.count;
      //       this.loading = false;
      //     })
      //     .catch((res) => {
      //       this.loading = false;
      //       this.$Message.error(res.msg);
      //     });
      // } else {
      //   liveGoods({
      //     is_show: "1",
      //     status: "1",
      //     live_id: this.datas.id,
      //     kerword: this.formValidate.store_name,
      //     page: this.formValidate.page,
      //     limit: this.formValidate.limit
      //   })
      //     .then(async (res) => {
      //       let data = res.data;
      //       data.list.forEach((el) => {
      //         el.image = el.cover_img;
      //       });
      //       this.tableList = data.list;
      //       this.total = res.data.count;
      //       this.loading = false;
      //     })
      //     .catch((res) => {
      //       this.loading = false;
      //       this.$Message.error(res.msg);
      //     });
      // }
    },
    ok() {
      if (this.images.length > 0) {
        // if (this.$route.query.fodder === 'image') {
        //   let imageValue = form_create_helper.get('image');
        //   form_create_helper.set('image', imageValue.concat(this.images));
        //   form_create_helper.close('image');
        // } else {
        //   if (this.isdiy) {
        //     this.$emit('getRoleAdminId', this.diyVal);
        //   } else {
        //     this.$emit('getRoleAdminId', this.images);
        //   }
        // }
        this.$emit("getRoleAdminId", this.images);
      } else {
        this.$Message.warning("请先选择用户");
      }
    },
    // 表格搜索
    userSearchs() {
      this.currentid = 0;
      this.productRow = {};
      this.formValidate.page = 1;
      if (this.diy) {
        this.productList();
      } else {
        this.getList();
      }
    },
    clear() {
      this.productRow.id = "";
      this.currentid = "";
    }
  }
};
</script>

<style scoped lang="stylus">
.footer {
  margin: 15px 0;
}

.tabBox_img {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  cursor: pointer;

  img {
    width: 100%;
    height: 100%;
  }
}

.tabform {
  >>> .ivu-form-item {
    margin-bottom: 16px !important;
  }
}

.btn {
  margin-top: 20px;
  float: right;
}

.goodList {
  >>> table {
    width: 100% !important;
  }
}
</style>
